<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

// [ 应用入口文件 ]
namespace think;

use app\model\UnitModel;
use app\model\UserModel;

require __DIR__ . '/vendor/autoload.php';
$app = new App();
$http = $app->http;
$http->run();

$UnitModel = new UnitModel();
$UserModel = new UserModel();
$UnitModel->startTrans();
try {
    $data = $UnitModel->field('id,name')->select()->toArray();
    foreach ($data as $item){
        $user_num = $UserModel->where([
            ['unit_id', '=', $item['id']],
            ['role_mode','=',3]
        ])->count('id');
        $UnitModel->where('id','=',$item['id'])->update([
            'add_user_num'=>$user_num
        ]);
    }
    $UnitModel->commit();
    echo '恢复成功！';
} catch (\Exception $e) {
    $UnitModel->rollback();
    echo error($e->getMessage(), $e->getCode(),[
        'line' => $e->getLine(),
        'file' => $e->getFile(),
    ]);
}